package com.chanyachting.emall.action.companyproduct;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.domain.Companyproduct;
import com.chanyachting.emall.domain.Member;
import com.chanyachting.emall.domain.ServiceOptionType;
import com.chanyachting.emall.service.CompanyproductService;
import com.chanyachting.emall.service.MemberService;
import com.chanyachting.emall.serviceImpl.CompanyproductServiceImpl;
import com.chanyachting.emall.serviceImpl.MemberorderservicesBO;
import com.chanyachting.emall.util.DateUtil;
import com.chanyachting.emall.util.RequestUtil;

public class PublicAction extends Action {
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	
		RequestUtil ru=new RequestUtil(request);
		
		int cpid=Integer.parseInt(ru.getParameter("cpid", "0"));

		Companyproduct cp=new Companyproduct();
		
		cp.setId(cpid);
		
		CompanyproductService cps=new CompanyproductServiceImpl();
		
		cp=cps.getOneCompanyproduct(cp);
		
		//检查用户是否有对应功能权限
		Member member=(Member)ru.getSessionObject(MemberService.MEMBER_IN_SESSION, new Member());
		
		String optionType=ServiceOptionType.Product_Recommended;
		
		boolean temp=MemberorderservicesBO.checkMmemberServiceByMemberAndOPType(member, optionType);
		
		response.setContentType("text/html;charset=UTF-8"); 
		
		if(temp){
			
			cp.setUpdateDate(DateUtil.getDate());
			temp=cps.publicCompanyproduct(cp);
			
			if(temp){
				
				ru.setMsg("提交成功，请到审核一览中查看结果。");
			} else {
				ru.setMsg("提交失败！可能数据存在问题，请仔细检查。");
			}
			
		}else{
			
			ru.setMsg("推荐失败！请购买/开启新闻活动推荐服务。");
		}
		
		return mapping.findForward("publishProduct");
	}
}
